package cn.mianshiyi.monitor;


import java.util.concurrent.atomic.AtomicLong;

/**
 * 统计平均耗时
 * @author shangqing.liu
 */
public class AtomicTimeCounter extends MonitorCounter {

    private final AtomicLong counter = new AtomicLong(0);
    private final AtomicLong timer = new AtomicLong(0);

    @Override
    public Number getValueAndReset() {
        if (counter.get() == 0) {
            return 0;
        }
        return timer.getAndSet(0) / counter.getAndSet(0);
    }

    @Override
    public void addValue(Number value) {
        timer.addAndGet(value.longValue());
        counter.incrementAndGet();
    }

}
